package com.yunc.base.security.service;

import cn.hutool.core.collection.CollUtil;
import com.yunc.base.security.UserEntity;
import com.yunc.modules.sys.entity.SysUser;
import com.yunc.modules.sys.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * password模式user登录实现
 *
 * @author taocongcong
 * @create 2019-06-24 16:47
 */
@Service("userDetailsService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserDetailsServiceImpl implements UserDetailsService {

    @Value("${data.scope.flag:false}")
    private Boolean dataScopeFlag;

    private final ISysRoleService sysRoleService;
    private final ISysUserService sysUserService;
    private final ISysUserRoleService sysUserRoleService;
    private final ISysRoleDeptService sysRoleDeptService;
    private final ISysDeptService sysDeptService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByLoginName(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("未查询到用户");
        }
        // 拥有权限,在自定义需要割断
        List<String> permsList = sysUserService.queryUserMenuPermsList(sysUser.getId());
        // 拥有的角色，查询出角色所拥有的最大范围
        List<String> roleIdList = sysUserRoleService.listRoleList(sysUser.getId());
        List<String> roles = roleIdList.stream().map(a -> String.valueOf(a)).collect(Collectors.toList());
        UserEntity user = new UserEntity(sysUser.getId(), sysUser.getLoginName(), permsList, roles);
        int dataScope = 1;
        if (dataScopeFlag) {
            // 是否还有全部权限
            dataScope = sysRoleService.getDataScopeAll(sysUser.getId());
            // 拥有的角色数据权限-部门
            List<String> deptIds = sysRoleDeptService.listDept(sysUser.getId());
            // 本部门及以下权限，有code可以直接like出子节点
            List<String> deptCodeList = sysRoleDeptService.listDeptChild(sysUser.getId());
            if (CollUtil.isNotEmpty(deptCodeList)) {
                List<String> childDeptIds = sysDeptService.listDeptByCode(deptCodeList);
                // 合并
                if (CollUtil.isEmpty(deptIds) && CollUtil.isNotEmpty(childDeptIds)) {
                    deptIds = childDeptIds;
                } else if (CollUtil.isNotEmpty(deptIds) && CollUtil.isNotEmpty(childDeptIds)) {
                    for (String deptId : childDeptIds) {
                        if (!deptIds.contains(deptId)) {
                            deptIds.add(deptId);
                        }
                    }
                }
            }
            user.setDeptIds(deptIds);
        }
        user.setDataScope(dataScope);
        user.setHeadLogo(sysUser.getHeadLogo());
        user.setPassword(sysUser.getUserPassword());
        return user;
    }
}
